﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ModernUIApp1
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class LoginWindow : Window
	{
		public LoginWindow()
		{
			InitializeComponent();
		}

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            WcfConnection wcfConnection = new WcfConnection();

            ModernUIApp1.WcfConnection.AsyncMethodCaller caller = new ModernUIApp1.WcfConnection.AsyncMethodCaller(wcfConnection.validateUser);

            IAsyncResult result = caller.BeginInvoke(usernameTextBox.Text, passwordBox.Password, null, null);
            
            bool valid = caller.EndInvoke(result);
            spinner.Visibility = System.Windows.Visibility.Visible;
            System.Threading.Thread.Sleep(3000);
            if (valid)
            {
                ApplicationState.SetValue("id", wcfConnection.getUserId(usernameTextBox.Text));
                ApplicationState.SetValue("username", usernameTextBox.Text);
                ApplicationState.SetValue("password", passwordBox.Password);
                ApplicationState.SetValue("validUser", "true");
                var menuWindow = new MainWindow();
                menuWindow.Show();
                this.Close();
            }
            else
            {
                spinner.Visibility = System.Windows.Visibility.Collapsed;
            }

        }
	}
}
